<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use DB;
use Hash;
use Illuminate\Http\Request;

class adminController extends Controller {
	/**
	 * Display a listing of the resource.
	 *
	 * @return \Illuminate\Http\Response
	 */
	public function login() {
		return view('admin.login');
	}

	/**
	 * 显示后台主页
	 */
	public function index() {
		return view('admin.layout', ['title' => '后台管理']);
	}

	/**
	 * 处理后台登录
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function doLogin(Request $request) {
		$user = DB::table('users')->where('username', $request->name)->first();
		if (Hash::check($request->passwd, $user->password)) {
			$auth = ['普通用户', '前台管理员', '后台管理员', '超级管理员'];
			session([
				'id' => $user->id,
				'username' => $user->username,
				'name' => $user->name,
				'face' => $user->face,
				'auth' => $auth[$user->auth - 1],
				'isAdmin' => 1,
			]);
			// $request->session()->put('username', $user->username);
			// $request->session()->put('face', $user->face);
			// $request->session()->put('auth', $auth[$user->auth]);
			return redirect('/admin');
		}
	}
	public function logout(Request $request) {
		$request->session()->forget('username');
		$request->session()->forget('face');
		$request->session()->forget('auth');
		$request->session()->forget('id');
		$request->session()->forget('isAdmin');
		return back();
	}
}
